//
//  NILoadingView.m
//  gweAD
//
//  Created by navinfo air on 2016/11/21.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "NILoadingView.h"
#import "TagDefines.h"

@implementation NILoadingView{
    __weak IBOutlet UIView *contentView;
    
    __weak IBOutlet UIImageView *successImageView;
    __weak IBOutlet UIImageView *loadingImageView;
    __weak IBOutlet UIImageView *failedImageView;
    
    __weak IBOutlet UILabel *textLabel;
    BOOL animating;
}


-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    
    textLabel.numberOfLines = 0;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    if (!animating) {
        [self startAnimate];
    }
}

- (void)startAnimate{
    animating = YES;
    //动画
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
    rotationAnimation.toValue = [NSNumber numberWithFloat:2.0*M_PI];
    rotationAnimation.autoreverses = NO;
    rotationAnimation.repeatCount = MAXFLOAT;
    rotationAnimation.duration = 1.0f;
    rotationAnimation.removedOnCompletion = NO;
    
    [loadingImageView.layer addAnimation:rotationAnimation forKey:@"basic"];
}

- (void)setText:(NSString *)text{
    textLabel.text = text;
}

- (void)changeToSuccessWithText:(NSString *)text{
    [loadingImageView.layer removeAllAnimations];
    loadingImageView.hidden = YES;
    failedImageView.hidden = YES;
    
    successImageView.hidden = NO;
    textLabel.text = text;
    
}
- (void)changeToFailedWithText:(NSString *)text{
    [loadingImageView.layer removeAllAnimations];
    loadingImageView.hidden = YES;
    successImageView.hidden = YES;
    
    failedImageView.hidden = NO;
    textLabel.text = text;
    
}

+ (void)closeWithText:(BOOL)success textForView:(nonnull NSString *)text completion:(void (^_Nullable)(void))completion{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *view = [window viewWithTag:TAG_LOADINGINWINDOW];
        if (success) {
            [(NILoadingView *)view changeToSuccessWithText:text];
        }else{
            [(NILoadingView *)view changeToFailedWithText:text];
        }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [NILoadingView close];
        completion();
    });
}

+ (void)closeWithText:(BOOL)success textForView:(nonnull NSString *)text{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window viewWithTag:TAG_LOADINGINWINDOW];
    if (success) {
        [(NILoadingView *)view changeToSuccessWithText:text];
    }else{
        [(NILoadingView *)view changeToFailedWithText:text];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [NILoadingView close];
    });
}

+ (void)show:(nonnull NSString *)text{
    [self close];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    NILoadingView *loadingView = [[NILoadingView alloc] init];
    [loadingView awakeFromNib];
    loadingView.frame = [[[UIApplication sharedApplication] keyWindow] bounds];
    [loadingView setBackgroundColor:[UIColor clearColor]];
    [window addSubview:loadingView];
    [loadingView setTag:TAG_LOADINGINWINDOW];
    
    NSArray *viewTags = @[@TAG_NOTIFICATIONINWINDOW,@TAG_MENUINWINDOW,@TAG_SECURITYPASSWORDINWINDOW,@TAG_SECURITYPASSWORDBGINWINDOW,@TAG_LOADINGINWINDOW];
    NSNumber *number;
    for (int i = 0; i < viewTags.count; i++) {
        number = [viewTags objectAtIndex:i];
        if ([window viewWithTag:number.integerValue]) {
            [window bringSubviewToFront:[window viewWithTag:number.integerValue]];
        }
    }
    
    
    
    [loadingView startAnimate];
    if (text) {
        [loadingView setText:text];
    }
}

+ (void)close{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window viewWithTag:TAG_LOADINGINWINDOW];
    if (view) {
        [view removeFromSuperview];
    }
}


@end
